package com.gtenacg.fileservice.controller;


import com.gtenacg.fileservice.response.Result;
import com.gtenacg.fileservice.util.FileUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Slf4j
@RestController
@RequiredArgsConstructor
public class FileModificationController {

    @Value("${file-path}")
    String filePath;

    final FileUtil fileUtil;

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) {
        log.info("fileName: {}", file.getOriginalFilename());
        log.info("fileSize: {}", file.getSize());
        log.info("fileType: {}", file.getContentType());
        log.info("fileContent: {}", file.getContentType());
        log.info("path: {}", path);

        String filePath = this.filePath + "/" + path;

        try {
            File f = new File(filePath);
            if (!f.getParentFile().exists()) {
                boolean mkdirs = f.getParentFile().mkdirs();
                if (!mkdirs) {
                    throw new IOException("创建目录失败: " + f.getParentFile().getAbsolutePath());
                }
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));

            InputStream is = file.getInputStream();
            FileCopyUtils.copy(is, bos);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }

    @GetMapping("/mkdirs")
    public Result mkdirs(@RequestParam String path) {
        log.info("新建文件夹: {}", path);
        String filePath = this.filePath + "/" + path;
        File f = new File(filePath);
        try {
            if (f.exists()) {
                String msg = "目录 '" + path + "' 已存在，无法新建！";
                log.warn(msg);
                return new Result(HttpStatus.CONFLICT.value(), msg, null);
            }

            boolean mkdirs = f.mkdirs();
            if (!mkdirs) {
                throw new IOException("创建目录失败: " + filePath);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam String path) {
        log.info("删除: {}", path);

        String filePath = this.filePath + "/" + path;

        boolean delete = fileUtil.deleteDirectory(new File(filePath));
        if (!delete) {
            String msg = "删除失败: " + path;
            log.warn(msg);
            return new Result(HttpStatus.CONFLICT.value(), msg, null);
        }
        return Result.success();
    }





//    @PutMapping("/toZip")
    public Result toZip(@RequestParam String path) {
        log.info("压缩: {}", path);
        String filePath = this.filePath + "/" + path;
        try {
            fileUtil.toZip(new File(filePath));
        } catch (IOException e) {
            log.error("压缩失败: {}", path);
            e.printStackTrace();
            return new Result(HttpStatus.INTERNAL_SERVER_ERROR.value(), "压缩失败: " + path + ", 异常已被记录, 请稍后重试", null);
        }
        return Result.success();
    }

//    @PutMapping("/unzip")
    public Result unzip(@RequestParam String path) {
        log.info("解压: {}", path);
        String filePath = this.filePath + "/" + path;
        try {
            File src = new File(filePath);
            fileUtil.unzip(src, src.getParentFile());
        } catch (IOException e) {
            log.error("解压失败: {}", path);
            e.printStackTrace();
            return new Result(HttpStatus.INTERNAL_SERVER_ERROR.value(), "解压失败: " + path + ", 异常已被记录, 请稍后重试", null);
        }
        return Result.success();
    }

}
